package com.stone.base.controller;

import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.stone.back.user.entity.BfUser;
import com.stone.common.util.DateUtil;
import com.stone.exception.UnSafeOperationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 控制器基类
 */
@Controller
public abstract class BaseController{
	//操作成功返回
	public String SUCCESS = "success";
    //错误页面
	public String ERROR = "error";
    //失败页面
	public String FAIL = "fail";
	//信息页显示内容
	public final String I18NLANGUAGE = SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME;
	//信息页显示内容
	public final String CNINFO = "cninfo";
	public final String USINFO = "usinfo";
	//后台用户信息(session中用户信息的key)
	public final String BACKUSER = "backCurrentUser";
	//前台用户信息(session中用户信息的key)
	public final String FRONTUSER = "frontCurrentUser";
	//下单重复提交标示
	public final String ORDERSUBMITMARK = "orderSubmitMark";
	//注册重复提交标示
	public final String REGISTERSUBMITMARK = "registerSubmitMark";
	//购物车
	public final String SHOPCAR = "shopCar";
	//个人购物车
	public final String PERSIONSHOPCAR = "persionShopCar";
	//购物车价格
	public final String SHOPCARPRICE = "shopCarPrice";
	//订单标示
	public final String ORDER = "order";
	
	/**
	 * 获取后台当前用户信息
	 * @param request
	 * @return
	 */
	public BfUser getBackCurrentUser(HttpServletRequest request) {
		Object userObj = request.getSession().getAttribute(this.BACKUSER);
		BfUser user = null;
		if(null != userObj) {
			user = (BfUser) userObj;
		}
		return user;
	}
	
	/**
	 * 获取前台当前用户信息
	 * @param request
	 * @return
	 */
	public BfUser getFrontCurrentUser(HttpServletRequest request) {
		Object userObj = request.getSession().getAttribute(this.FRONTUSER);
		BfUser user = null;
		if(null != userObj) {
			user = (BfUser) userObj;
		}
		return user;
	}
        
        public void setFrontCurrentUser(BfUser user, HttpServletRequest request) {
            if(user == null)
                return;
            request.getSession().setAttribute(this.FRONTUSER, user);
        }
	
	/**
	 * String绑定Date类型转换防范
	 * @param request
	 * @param binder
	 * @throws Exception
	 */
	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {  
		//对于需要转换为Date类型的属性，使用DateEditor进行处理  
		binder.registerCustomEditor(Date.class, new DateEditor());
	} 
	
	class DateEditor extends PropertyEditorSupport {  
	    private DateFormat dateFormat = new SimpleDateFormat(DateUtil.DATETIME_FORMAT);  
	    private boolean allowEmpty = true;  
	  
	    public DateEditor() {  
	    }  
	  
	    public DateEditor(DateFormat dateFormat) {  
	        this.dateFormat = dateFormat;  
	    }  
	  
	    public DateEditor(DateFormat dateFormat, boolean allowEmpty) {  
	        this.dateFormat = dateFormat;  
	        this.allowEmpty = allowEmpty;  
	    }  
	  
	    /** 
	     * Parse the Date from the given text, using the specified DateFormat. 
	     */
	    @Override
	    public void setAsText(String text) throws IllegalArgumentException {
	        if (this.allowEmpty && StringUtils.isBlank(text)) {
	            // Treat empty String as null value.
	            setValue(null);
	        } else {
	            if(text.contains(":") && text.contains("-")) {
				    setValue(DateUtil.strFormatDate(DateUtil.DATETIME_FORMAT, text));
				} else if(text.contains(":")) {
				    setValue(DateUtil.strFormatDate(DateUtil.TIME_FORMAT, text));
				} else {
				    setValue(DateUtil.strFormatDate(DateUtil.DATE_FORMAT, text));
				}
	        }
	    }
	  
	    /** 
	     * Format the Date as String, using the specified DateFormat. 
	     */
	    @Override
	    public String getAsText() {
	        Date value = (Date) getValue();
	        DateFormat dateFormat = this.dateFormat;
	        return (value != null ? dateFormat.format(value) : "");
	    }
	}
        
        /**
         * 在出现不安全操作调用时出发
         */
        @ExceptionHandler(UnSafeOperationException.class)
        @ResponseStatus(HttpStatus.METHOD_FAILURE)
        public void unSafeOperationHandler() {
        
        }
        
}